<?php
 /*商铺控制器*/ 
 class StoreController extends Controller{
 	private $model;

 	public function __init(){
 		$this->model = K('Store');
 	}

 	// 列表页面展示
 	public function index(){

 		$allstore = $this->model->get_data();
 		

 		$this->assign('allstore',$allstore);
 		$this->display();
 	}
/**
 * [add 添加商铺]
 */
 	public function add(){


 		if(IS_POST){
 			

 			if(!$this->model->create()) $this->error($this->model->error);

 			if(!$this->model->add_store()){
 				$this->error($this->model->error);
 				
 			}
 			$this->success("添加成功",U('index'));
 		}
 		// 获得所有的类型
 		// 
 		//获得 标签
 		$tagName = K('Tag')->get_data();
 		$colums = K('Columns')->get_data();

 		//获得所有的地区
 		$district = K('District')->get_data();



 		$district= Data::tree($district,'dname','did','pid');
 		$this->assign('district',$district);
 		$this->assign('tagName',$tagName);
 		$this->assign('colums',$colums);
 		
 		$this->display();
 	}


 	public function del(){
 		if(IS_GET){
 			$stid = Q('get.stid',0,'intval');
 			if(!$this->model->drop_db(array('stid'=>$stid))){
 				$this->error($this->model->error);
 			}
 			

 			$this->success('删除成功',U('index'));
 		}



 		
 	}

 	public function edit_cate(){
 		if(IS_POST){
 			$stid = Q('post.stid',0,'intval');
 			if(!$this->model->create()) $this->error($this->model->error);
 			$this->model->alter_data(array('stid'=>$stid));
 			// 标签(清楚再添加)
			K('Tagstore')->delTag_data(array('stid'=>$stid));

			foreach (Q('post.attr') as $v) {
			
			$TagData = array(
				'stid'=> $stid,
				'tid' => $v,
				
				);

				K('Tagstore')->add_data($TagData);
			}

 			$this->success('修改成功',U('index'));


 		}
 		

 		// 获得所有的类型
 		$colums = K('Columns')->get_data();

 		//获得所有的地区
 		$district = K('District')->get_data();

 		// 获得所有的便签

 		$tagName = K('Tag')->get_data();

 		$stid = Q('get.stid',0,'intval');
 		
 		//获得原来的标签
 		$tagArr = K('Tagstore')->get_data(array('stid'=>$stid));

 		$nTagarr = array();
 		if(!empty($tagArr)){
		foreach ($tagArr as $v) {
			$nTagarr[] = $v['tid'];
			}
		}
 		$this->assign('tagArr',$nTagarr);


 		$district= Data::tree($district,'dname','did','pid');
 		//取出旧数据
 		$defaultData = $this->model->get_one_data(array('stid'=>$stid));

 		$array = explode(',', $defaultData['favorable']);


 		$this->assign('defaultData',$defaultData);

 		$this->assign('colums',$colums);

 		$this->assign('district',$district);

 		$this->assign('tagName',$tagName);
 		$this->assign('array',$array);

 		$this->display();
 	}

 }





 ?>